//
//  commentView.m
//  garbageclient
//
//  Created by Wang Wu on 11-8-11.
//  Copyright 2011年 __MyCompanyName__. All rights reserved.
//

#import "commentView.h"
#import "commondef.h"
#import "SBJson.h"

@implementation commentView

@synthesize delegate;

- (void)dealloc
{
    [navBar release];
    [textfield release];
    
    [super dealloc];
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        CGRect screenrc = [[UIScreen mainScreen] applicationFrame];
        
        navBar = [[UINavigationBar alloc] initWithFrame: CGRectMake(0.0f, 0.0f, screenrc.size.width, NAV_HEIGHT)];
        
        [self addSubview: navBar];
        
        UINavigationItem *navItem = [[UINavigationItem alloc] initWithTitle: @"评论"];
        UIBarButtonItem *rightbtn = [[UIBarButtonItem alloc] initWithTitle:@"发送" 
                                                                     style:UIBarButtonItemStylePlain
                                                                    target:self 
                                                                    action:@selector(send:)];
        navItem.rightBarButtonItem = rightbtn;
        
        UIBarButtonItem *leftbtn = [[UIBarButtonItem alloc] initWithTitle:@"返回" 
                                                                    style:UIBarButtonItemStylePlain
                                                                   target:self 
                                                                   action:@selector(backtodetailsview:)];
        navItem.leftBarButtonItem = leftbtn;
        [navBar pushNavigationItem:navItem animated:YES];
        
        [navItem release];
        [rightbtn release];
        
        textfield = [[UITextField alloc] initWithFrame:CGRectMake(0.0,
                                                                  0.0f + NAV_HEIGHT, 
                                                                  screenrc.size.width,
                                                                  screenrc.size.height - 20.0f - NAV_HEIGHT)];
        [self addSubview:textfield];
        textfield.delegate = self;
        [textfield becomeFirstResponder];
        textfield.backgroundColor = [UIColor whiteColor];
        textfield.returnKeyType = UIReturnKeyDone;
    }
    return self;
}

-(void)backtodetailsview:(id)sender
{
    [delegate commentback];
}

-(void)send:(id)sender
{
    //http://www.mwinphone.com/?json=respond.submit_comment
    // 指定提交的URL地址
    NSString *strurl = [[NSString alloc] initWithFormat:@"http://www.mwinphone.com/?json=respond.submit_comment&post_id=1319&name=gump&email=85150091@qq.com&content=%@",textfield.text];
    
    NSURL *url = [NSURL URLWithString:strurl];
    // 创建NSMutableURLRequest
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
//    // 设置HTTP头信息
//    NSString *boundary = [[NSProcessInfo processInfo] globallyUniqueString];
//    NSString *boundaryString = [NSString stringWithFormat: 
//                                @"multipart/form-data; boundary=%@", boundary];
//    [request addValue: boundaryString forHTTPHeaderField: @"Content-Type"];
//    // 根据REST协议指定HTTPMethod为POST
//    [request setHTTPMethod:@"POST"];
//    // 将要提交的数据转换成JSON格式后设置为HTTPBody
//    
//    NSDictionary *items = [NSDictionary dictionaryWithObjectsAndKeys:
//                           
//                           [[NSNumber alloc] initWithInt:1319], @"post_id", @"gump", @"name",
//                           
//                           @"yige2002@tom.com", @"email", , @"content", nil];
//    NSString *JSONString = [items JSONRepresentation];
//    [request setHTTPBody:[JSONString dataUsingEncoding:NSASCIIStringEncoding]];
     // 定义HTTP将要返回的Response对象
    NSURLResponse *response = nil;
    NSError *error = nil;
    
    // 发出同步请求
    NSData *result = [NSURLConnection sendSynchronousRequest:request 
                                           returningResponse:&response error:&error];
    
    [request release];
    // 发生错误
    if (error) {
        UIAlertView *errorAlertView = [[UIAlertView alloc] initWithTitle:@"Error" 
                                                                 message:[error localizedDescription] 
                                                                delegate:nil cancelButtonTitle:@"Close" 
                                                       otherButtonTitles:nil];
        [errorAlertView show];
        [errorAlertView release];
    }
    // 返回的结果
    NSString *resultString = [[NSString alloc] initWithData:result 
                                                   encoding:NSUTF8StringEncoding];
    [resultString release];
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textfield resignFirstResponder];
    return YES;
}
@end
